package com.it.gmall.order.config;

import com.it.gmall.mq.consts.MqConst;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

// 超过两个小时没有下单 发送基于插件的延迟消息
// 取消订单的配置
@Configuration
public class CancelOrderDelayMsgConfig {

    // 交换机
    @Bean
    public Exchange cancelOrderDelayExchange(){
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-delayed-type","direct");
        return new CustomExchange(MqConst.EXCHANGE_DIRECT_ORDER_CANCEL,"x-delayed-message",true,false,arguments);
    }

    // 队列
    @Bean
    public Queue cancelOrderDelayQueue(){
        return QueueBuilder.durable(MqConst.QUEUE_ORDER_CANCEL).build();
    }

    // 绑定交换机和队列
    @Bean
    public Binding bindingOrderDelayExchangeAndQueue(
            @Qualifier(value = "cancelOrderDelayQueue") Queue cancelOrderDelayQueue,
            @Qualifier(value = "cancelOrderDelayExchange") Exchange cancelOrderDelayExchange
    ){
        return BindingBuilder.bind(cancelOrderDelayQueue).to(cancelOrderDelayExchange).with(MqConst.ROUTING_ORDER_CANCEL).noargs();
    }
}
